/**************************************************************************
 * Copyright (c), Caper 2013 
 *
 *
 *
 * Arquivo Original :Chat.java
 * Projeto Original : chat
 * Author: carlos_dias
 * Criação : 09/07/2013 - 22:47:50
 **************************************************************************
 * Resumo:
 *
 * Problemas Conhecidos:
 *
 * Erros falhas e exceções:
 * 
 *************************************************************************/
package br.com.carper.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.InitialContext;

/**
 * @author carlos_dias
 * 
 */
public class Chat implements MessageListener {
    private TopicSession pubSession;
    private TopicPublisher publisher;
    private TopicConnection connection;
    private String username;

    public Chat(String topicFactory, String topicName, String username) throws Exception {

        InitialContext ctx = new InitialContext();
        TopicConnectionFactory conFactory = (TopicConnectionFactory) ctx.lookup(topicFactory);
        TopicConnection connection = conFactory.createTopicConnection();
        TopicSession pubSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        TopicSession subSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic chatTopic = (Topic) ctx.lookup(topicName);
        TopicPublisher publisher = pubSession.createPublisher(chatTopic);
        TopicSubscriber subscriber = subSession.createSubscriber(chatTopic, null, true);

        subscriber.setMessageListener(this);
        
        this.connection = connection;

        this.pubSession = pubSession;
        this.publisher = publisher;
        this.username = username;
        connection.start();
    }


    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            System.out.println(textMessage.getText());
        } catch (JMSException jmse) {
            jmse.printStackTrace();
        }
    }
    protected void writeMessage(String text) throws JMSException {
        TextMessage message = pubSession.createTextMessage();
        message.setText(username + ": " + text);
        publisher.publish(message);
    }

    public void close() throws JMSException {
        connection.close();
    }

    public static void main(String[] args) {
        try {
            if (args.length != 3)
                System.out.println("Factory, Topic, or username missing");
           
            Chat chat = new Chat(args[0], args[1], args[2]);
            BufferedReader commandLine = new java.io.BufferedReader( new InputStreamReader(System.in));
            while (true) {
                String s = commandLine.readLine();
                if (s.equalsIgnoreCase("exit")) {
                    chat.close();
                    System.exit(0);
                } else{
                    chat.writeMessage(s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
